package com.huluobo.a2112mvvmdouyindemo.live;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm_base.bean.FollowBeanResult;
import com.huluobo.mvvm_base.bean.GiftBeanResult;
import com.huluobo.mvvm_base.bean.Money;
import com.huluobo.mvvm_base.model.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/4/1.
 */
public class LiveRepo extends BaseRepo {

    public void follow(RequestBody body, MutableLiveData<FollowBeanResult> success, MutableLiveData<String> failed) {
        apiService.followAuth(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowBeanResult followBeanResult) {
                        success.setValue(followBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void noFollowAuth(RequestBody body, MutableLiveData<FollowBeanResult> success,
                             MutableLiveData<String> failed) {
        apiService.noFollowAuth(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowBeanResult followBeanResult) {
                        success.setValue(followBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getGiftList(MutableLiveData<GiftBeanResult> success, MutableLiveData<String> failed) {
        apiService.getGiftList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GiftBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GiftBeanResult giftBeanResult) {
                        success.setValue(giftBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void queryUserMoney(String username, MutableLiveData<Money> success, MutableLiveData<String> failed) {
//        if (moneyDao.queryUserMoney(username) != null) {
//            success.setValue(moneyDao.queryUserMoney(username));
//        } else {
//            failed.setValue("查询用户余额失败");
//        }
        moneyDao.queryUserMoney(username)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Money>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Money money) {
                        success.setValue(money);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateUserMoney(Money money) {
        moneyDao.updateUserMoney(money);
    }
}
